<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<html>
<head>
    <title>管理用户信息</title>
    <script src="${pageContext.request.contextPath}/resources/js/jquery-2.1.1.min.js"></script>
</head>
<body>
<c:if test="${sessionScope.isLogin}">
    <div>当前登陆人: ${sessionScope.loginUser.nickname}</div>
</c:if>
<c:if test="${!sessionScope.isLogin}">
    <a href="${pageContext.request.contextPath}/login.jsp">登录</a>
</c:if>

<table>
    <thead>
    <tr>
        <th>用户名</th>
        <th>昵称</th>
        <th>年龄</th>
        <th>生日</th>
        <th>操作</th>
    </tr>
    </thead>
    <tbody>
    <c:forEach items="${studentData}" var="item">
        <tr>
            <td>${item.value.username}</td>
            <td>${item.value.nickname}</td>
            <td>
                <c:choose>
                    <c:when test="${item.value.age < 18}">未成年</c:when>
                    <c:when test="${item.value.age < 30}">青年</c:when>
                    <c:when test="${item.value.age < 50}">中年</c:when>
                    <c:otherwise>老年</c:otherwise>
                </c:choose>
            </td>
            <td>
                <fmt:formatDate value="${item.value.birthday}" pattern="yyyy年MM月dd日" />
            </td>
            <td>
                <a href="${pageContext.request.contextPath}/student/edit?username=${item.key}">编辑</a>
                <button onclick="delStu(${item.key})">删除</button>
            </td>
        </tr>
    </c:forEach>
    </tbody>
    <script>
        function delStu(username) {
            // 获取这个用户名之后,调用接口
            $.ajax({
                url:"${pageContext.request.contextPath}/student?username="+username,
                type:"delete",
                success(res){
                    console.log(res);
                    if(res.success){
                        alert("删除成功")
                        location.reload();
                    }
                }
            })
        }
    </script>
</table>
</body>
</html>
